package com.github.tvbox.osc.ui.activity;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.tvbox.osc.R;
import com.github.tvbox.osc.base.App;
import com.github.tvbox.osc.base.BaseActivity;
import com.github.tvbox.osc.bean.Daily;
import com.github.tvbox.osc.bean.Weather;
import com.github.tvbox.osc.picasso.RoundTransformation;
import com.github.tvbox.osc.util.DefaultConfig;
import com.github.tvbox.osc.util.MD5;
import com.github.tvbox.osc.util.Utils;
import com.github.tvbox.osc.viewmodel.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.orhanobut.hawk.Hawk;
import com.owen.tvrecyclerview.widget.TvRecyclerView;
import com.owen.tvrecyclerview.widget.V7GridLayoutManager;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 天气UI
 */
public class WeatherActivity extends BaseActivity {
    private ImageView ivWeather;

    private LinearLayout llWeatherBg;

    private TextView tv_city_name;

    private TextView tv_date;

    private TextView tv_week;

    private TextView tv_weather_life_information;

    private TextView tv_temperature;

    private ImageView iv_weather_img;

    private View lv_weather_symbol;

    private TextView update_weather;

    private TvRecyclerView rv_day_list;

    private List<Weather> mDatas = new ArrayList<>();

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_weather;
    }

    @Override
    protected void init() {
        initViews();
        String json = Hawk.get("home_weather_json", "");
        if (!TextUtils.isEmpty(json)) {
            JsonObject dataJson = getWeatherData(json);
            JsonObject realtimeJson = dataJson.getAsJsonObject("realtime");
            if (realtimeJson != null) {
                JsonObject skyconJson = realtimeJson.getAsJsonObject("skycon");
                initWeatherBg(skyconJson.get("code").getAsString(), json);
                tv_weather_life_information.setText(skyconJson.get("desc").getAsString());
                tv_temperature.setText(Utils.subZeroAndDot(realtimeJson.get("temperature").getAsString()));
            }
            JsonObject areaJson = dataJson.getAsJsonObject("area");
            if (areaJson != null) {
                tv_city_name.setText(areaJson.get("name").getAsString());
            }
            JsonObject dailyJson = dataJson.getAsJsonObject("daily");
            Daily daily = new Gson().fromJson(dailyJson, new TypeToken<Daily>() {
            }.getType());
            if (daily != null) {
                List<Weather> dats = daily.get4DayWeather();
                for (Weather weather : dats) {

                    mDatas.add(weather);
                }
                baseQuickAdapter.notifyDataSetChanged();
            }
            lv_weather_symbol.setVisibility(View.VISIBLE);
        }
    }

    private void initWeatherBg(String code, String json) {
        JsonObject jsonObject = getWeatherPic(json, code);
        setBg(jsonObject.get("pic").getAsString(), ivWeather);

        String startColor = jsonObject.get("start_color_24h").getAsString();
        String endColor = jsonObject.get("end_color_24h").getAsString();
        if (TextUtils.isEmpty(startColor) || TextUtils.isEmpty(endColor)) {
            int[] colors = new int[]{-0x98c64a, -0x13ff74};
            GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TR_BL, colors);
            llWeatherBg.setBackground(gradientDrawable);
        } else {
            int[] colors = new int[]{Color.parseColor(startColor.indexOf("#") > -1 ? startColor : "#" + startColor),
                    Color.parseColor(endColor.indexOf("#") > -1 ? endColor : "#" + endColor)};
            GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TR_BL, colors);
            llWeatherBg.setBackground(gradientDrawable);
        }
    }

    private void initWeatherDesc(String desc) {
        if (!TextUtils.isEmpty(desc)) {
            tv_weather_life_information.setText(desc);
            if (desc.indexOf("晴") > -1) {
                iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_qing));
//                current_weather.setBackground(getResources().getDrawable(R.drawable.background_sunshine));
            }
            if (desc.indexOf("雨") > -1) {
                if (desc.indexOf("小雨") > -1) {
                    iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_xiaoyu));
                } else if (desc.indexOf("中雨") > -1) {
                    iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_zhongyu));
                } else if (desc.indexOf("大雨") > -1) {
                    iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_dayu));
                } else {
                    iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_xiaoyu));
                }
//                current_weather.setBackground(getResources().getDrawable(R.drawable.background_rain));
            }
            if (desc.indexOf("阴") > -1) {
                iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_yin));
//                current_weather.setBackground(getResources().getDrawable(R.drawable.background_overcast));
            }
            if (desc.indexOf("多云") > -1) {
                iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_duoyun));
//                current_weather.setBackground(getResources().getDrawable(R.drawable.background_cloud));
            }
            if (desc.indexOf("雷") > -1 || desc.indexOf("电") > -1) {
                iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_leidian));
//                current_weather.setBackground(getResources().getDrawable(R.drawable.background_lightning));
            }
            if (desc.indexOf("霾") > -1) {
                iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_wumai));
//                current_weather.setBackground(getResources().getDrawable(R.drawable.background_haze));
            }
            if (desc.indexOf("雾") > -1) {
                iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_wumai));
//                current_weather.setBackground(getResources().getDrawable(R.drawable.background_fog));
            }
            if (desc.indexOf("沙尘") > -1) {
                iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_shachen));
//                current_weather.setBackground(getResources().getDrawable(R.drawable.background_fog));
            }
            if (desc.indexOf("雪") > -1) {
                if (desc.indexOf("小雪") > -1) {
                    iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_xiaoxue));
                } else if (desc.indexOf("中雪") > -1) {
                    iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_zhongxue));
                } else if (desc.indexOf("大雪") > -1) {
                    iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_daxue));
                } else {
                    iv_weather_img.setBackground(getResources().getDrawable(R.drawable.ic_weather_xiaoxue));
                }
//                current_weather.setBackground(getResources().getDrawable(R.drawable.background_snow));
            }
        }
    }

    private void initViews() {
        ivWeather = findViewById(R.id.ivWeather);
        llWeatherBg = findViewById(R.id.llWeatherBg);

        update_weather = findViewById(R.id.update_weather);
        tv_date = findViewById(R.id.tv_date);
        tv_week = findViewById(R.id.tv_week);
        tv_temperature = findViewById(R.id.tv_temperature);
        iv_weather_img = findViewById(R.id.iv_weather_img);
        tv_weather_life_information = findViewById(R.id.tv_weather_life_information);
        lv_weather_symbol = findViewById(R.id.lv_weather_symbol);
        tv_city_name = findViewById(R.id.tv_city_name);
        rv_day_list = findViewById(R.id.rv_day_list);


        rv_day_list.setLayoutManager(new V7GridLayoutManager(this, 5));
        initAdapter();

        String pic = Hawk.get("home_weather_pic", "");
        App.getInstance().setImagePath(pic, iv_weather_img, 95, 95, 0);

        tv_date.setText(Utils.getNowDate("yyyy.MM.dd"));
        tv_week.setText(Utils.getWeekStr());

        String requestDay = Hawk.get("home_weather_day", "");
        Date date = Utils.getDateFromStandardTime(requestDay, "yyyyMMddHHmm");
        if (date == null) {
            update_weather.setText("最近更新天气时间:" + Utils.getNowDate());
        } else {
            update_weather.setText("最近更新天气时间:" + Utils.getSpecifiedFormatTime(date, "yyyy-MM-dd HH:mm"));
        }
    }

    private BaseQuickAdapter<Weather, BaseViewHolder> baseQuickAdapter;

    private void initAdapter() {
        baseQuickAdapter = new BaseQuickAdapter<Weather, BaseViewHolder>(R.layout.item_view_weather, mDatas) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, Weather item) {
                helper.setText(R.id.weather_time, item.getMD());
                helper.setText(R.id.weather_information, item.getDesc());
                helper.setText(R.id.weather_temperature, item.getMax() + "/" + item.getMin());
//                String date = item.get("date");
//                if (item.get("date").indexOf("实时") > -1) {
//                    helper.setText(R.id.weather_time, date.substring(0, 2));
//                } else {
//                    helper.setText(R.id.weather_time, item.get("date"));
//                }
                ImageView weatherImg = helper.getView(R.id.weather_img);
                String weather_ = item.getDesc();
                if (weather_.indexOf("晴") > -1) {
                    weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_qing));
                }
                if (weather_.indexOf("雨") > -1) {
                    if (weather_.indexOf("小雨") > -1) {
                        weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_xiaoyu));
                    } else if (weather_.indexOf("中雨") > -1) {
                        weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_zhongyu));
                    } else if (weather_.indexOf("大雨") > -1) {
                        weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_dayu));
                    } else {
                        weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_xiaoyu));
                    }
                }
                if (weather_.indexOf("阴") > -1) {
                    weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_yin));
                }
                if (weather_.indexOf("多云") > -1) {
                    weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_duoyun));
                }
                if (weather_.indexOf("雷") > -1 || weather_.indexOf("电") > -1) {
                    weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_leidian));
                }
                if (weather_.indexOf("霾") > -1) {
                    weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_wumai));
                }
                if (weather_.indexOf("雾") > -1) {
                    weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_wumai));
                }
                if (weather_.indexOf("沙尘") > -1) {
                    weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_shachen));
                }
                if (weather_.indexOf("雪") > -1) {
                    if (weather_.indexOf("小雪") > -1) {
                        weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_xiaoxue));
                    } else if (weather_.indexOf("中雪") > -1) {
                        weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_zhongxue));
                    } else if (weather_.indexOf("大雪") > -1) {
                        weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_daxue));
                    } else {
                        weatherImg.setBackground(getResources().getDrawable(R.drawable.ic_weather_xiaoxue));
                    }
                }
//                helper.setText(R.id.weather_information, item.getDesc());
            }
        };
        rv_day_list.setAdapter(baseQuickAdapter);
    }

    private JsonObject getWeatherData(String json) {
        JsonObject infoJson = new Gson().fromJson(json, JsonObject.class);
        JsonObject dataJson = infoJson.getAsJsonObject("data");
        if (dataJson != null) {
            JsonObject weatherJson = dataJson.getAsJsonObject("weather");
            if (weatherJson != null) {
                JsonObject weatherDataJson = weatherJson.getAsJsonObject("data");
                if (weatherDataJson != null) {
                    return weatherDataJson;
                }
            }
        }
        return new JsonObject();
    }

    private String getWeatherCode(JsonObject jsonObject) {
        JsonObject areaJson = jsonObject.getAsJsonObject("realtime");
        if (areaJson != null) {
            JsonObject skyconJson = areaJson.getAsJsonObject("skycon");
            return skyconJson.get("code").getAsString();
        }
        return "";
    }

    private JsonObject getWeatherPic(String json, String code) {
        JsonObject infoJson = new Gson().fromJson(json, JsonObject.class);
        JsonObject dataJson = infoJson.getAsJsonObject("data");
        if (dataJson != null) {
            JsonObject weatherJson = dataJson.getAsJsonObject("weather");
            if (weatherJson != null) {
                JsonObject resJson = weatherJson.getAsJsonObject("res");
                if (resJson != null) {
                    JsonObject skyconJson = resJson.getAsJsonObject("skycon");
                    if (skyconJson != null) {
                        return skyconJson.getAsJsonObject(code);
                    }
                }
            }
        }
        return new JsonObject();
    }

    private void setBg(String path, ImageView imageView) {
        if (!TextUtils.isEmpty(path)) {
            Picasso.get()
                    .load(DefaultConfig.checkReplaceProxy(path))
                    .transform(new RoundTransformation(MD5.string2MD5(path + "position=0"))
                            .centerCorp(true))
                    .into(imageView);
        }
    }
}
